<?php
/**
* VirtueMart Full Category List Module for Joomla! and Virtuemart
* Helper class
* @author		Andrew Patton
* @version		1.1.1b
* @copyright	(C) 2010 Andrew Patton. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* This module is free software.
* See COPYRIGHT.php for copyright notices and details.
*/

class modVMFullCategoryList
{

	/**
	 * Gets all children of a parent category
	 *
	 * @access	public
	 * @param int $catPId (the category_id of desired parent category)
	 * @return array (object list with category id, name, if published, order, and parent id)
	 **/
	function vmFCLGetCatSiblings($catPId = 0, $params) {
		// This should probably be done differently, with something like: (taken from admin/com/com_vm/html/store.index.php)
		//$ps_product_category = new ps_product_category();
		//$category_childs = $ps_product_category->get_child_list(0);
		$db = new ps_DB;
		$query	= 'SELECT a.category_id, a.category_name, a.category_publish, a.list_order, f.category_parent_id, f.category_child_id'
		        . ' FROM #__{vm}_category AS a, #__{vm}_category_xref AS f'
		        . ' WHERE a.category_id = f.category_child_id AND f.category_parent_id='.$catPId
				. ( $params->get('pub_only') == 1 ? ' AND a.category_publish=\'Y\'' : '' )
				. ( !$catPId && $params->get('cat_filter') > 0 ? ' AND a.category_id=\''.$params->get('cat_filter').'\'' : '' )
		        . ' ORDER BY '. ( $params->get('ordering') == 'a_asc' ? '': 'a.list_order, ') .'a.category_name';
		$db->setQuery( $query);
		return $db->loadObjectList();
	}

	/**
	 * Gets the Itemid for main virtuemart link in the menu
	 *
	 * @access	public
	 * @return int (Itemid for VirtueMart menu item)
	 **/
	function vmFCLGetVMItemId($catid = false) {
		$menu = &JSite::getMenu(); // use getMenu() function and totally avoid DB queries
		// If generic VM itemid hasn't yet been set by this function:
		if (vmRequest::getInt('vmGenericItemid', -1) == -1) {
			$vmItemid = 0;
			$vmItemid1 = 0;
			$vmItemid2 = 0;
			$vmItemid3 = 0;
			foreach($menu->_items as $itemid => $item) {
				if (strpos($item->link, 'com_virtuemart') !== false) { // first virtuemart menu item
					// let's get the sure bet out of the way:
					if (strpos($item->params, "product_id=\ncategory_id=\nflypage=\npage=\n") !== false || strpos($item->params, "\npage=shop.index") !== false) { // then it's definitely the VM home menu item
						$vmItemid = $itemid;
						break;
					}
					elseif (strpos($item->params, "\npage=\n") && strpos($item->params, "product_id=\n") !== false && !$vmItemid1) { // if there's no VM page or product id specified & we haven't already found a likely option
						$vmItemid1 = ($vmItemid1 ? $vmItemid1 : $itemid );
					}
					elseif (!$item->parent) { // if it's a top-level menu item (more likely to be important)
						$vmItemid2 = ($vmItemid2 ? $vmItemid2 : $itemid );
					}
					else { // for any other kind of VM link (will only be used if ALL other options fail)
						$vmItemid3 = ($vmItemid3 ? $vmItemid3 : $itemid );
					}
				}
			}
			$vmItemid = ($vmItemid ? $vmItemid : ($vmItemid1 ? $vmItemid1 : ($vmItemid2 ? $vmItemid2 : $vmItemid3)));
			vmRequest::setVar('vmGenericItemid', $vmItemid);
		}
		// Now check for specific category menu item if a catid was passed:
		if ($catid) {
			foreach($menu->_items as $itemid => $item) {
				// if it's a virtuemart menu item and is the category page:
				if (strpos($item->link, 'com_virtuemart') !== false && strpos($item->params, "category_id=$catid") !== false) {
					return $itemid;
				}
			}
		}
		return vmRequest::getInt('vmGenericItemid');
	}

}

?>